#include "srv_head.h"

/**
 * do_get_cmd - 对下载文件命令功能进行处理函数
 * @sockfd: 连接到客户端的socket文件描述符
 * @cmd: 执行的命令字符串
 *
*/

void do_cli_get(int sockfd, char *cmd)
{
	
	int fd;
	int nbytes;
	char buffer[1024];
	char filename[N];
	int ret;
	int cnt = 0;
	bzero(filename, N);
	strncpy(filename,cmd, strlen(cmd));//main去除了\n
	printf("filename = %s\n", filename); 
	
	 fd = open(filename, O_RDONLY);  //以只读打开 失败返回-1
	 if(fd < 0)
 	{
 		perror("open");
		send(sockfd, "error", strlen("error"), 0);
		return;
 	}

	 //获取要下载的文件大小
	 struct stat st;
	 fstat (fd, &st);
	 int size =st.st_size;

	 bzero(buffer, N);
	 snprintf(buffer, sizeof(buffer), "%d", size);
	 send(sockfd, buffer, strlen(buffer), 0); //转换为字符串
	 usleep(1000);	//防止粘包


	 while(1) {
	 	bzero(buffer, 1024);
		nbytes = read(fd, buffer, 1024);  
	 	cnt = cnt + 1024;
		 printf("srv read download file  %d bytes\n", nbytes);
	 //	printf("testC %s",buffer);
	 	if(nbytes < 0)  //读取失败
 		{
 			perror("read filename");
			close(fd);
			close(sockfd);
			return;
 		}
		else if (nbytes == 0)  //读取结束
		{
			send(sockfd, buffer, nbytes, 0);
			break;
		}
		else
		{
			system("clear");
			ret = send(sockfd, buffer, nbytes, 0);  //把读取的的内容send出去
			printf("ret = %d\n", ret);
			if(ret < 0)
			{
				perror("sent @ get_cmd");
				close(fd);
				close(sockfd);
				return;
			}
			printf("the file is currently being transferred fd: %d... \n", sockfd);
			printf("--- "RED"%d bytes "NONE"of %d bytes in total---\n", cnt, size);
			//usleep(8000);
			
		}
		//sleep(1);

	 }
	 
	printf("Get Transfer Filename: %s success\n", filename);
	close(fd);
	close(sockfd);
	
}


/*
作业:
实现 

if (给的名字)

	ls  文件
	file_stat(文件)

else
	ls 目录
	dir=opendir(目录)
	while((dp=readdir(dir))!=NULL)
		file_stat(dp->d_name)



列出当前目录下文件或子目录名


*/


/*
#include "srv_head.h"



void file_stat(char *filename, char **fullfilename)
{
    int i;
    struct tm *pt;
    struct stat buf; //保存指定文件的属性对象buf
   
    char type;
	//printf("File Point11 = %p\n", fullfilename );
    bzero(*fullfilename, 1024);

    //获取指定文件的属性对象
    if (lstat(filename, &buf) < 0) {
        perror("lstat");
        exit(1);
    }
    //打印文件类型
    if (S_ISREG(buf.st_mode))
        type = '-';
    else if (S_ISDIR(buf.st_mode))
        type = 'd';
    else if (S_ISBLK(buf.st_mode))
        type = 'b';
    else if (S_ISCHR(buf.st_mode))
        type = 'c';
    else if (S_ISFIFO(buf.st_mode))
        type = 'p';
    else if (S_ISSOCK(buf.st_mode))
        type = 's';
    else if (S_ISLNK(buf.st_mode))
        type = 'l';

    **fullfilename = type;
    
    //打印权限
    char str[] = "xwr-";
	char temp[1024];
    for (i = 8; i >= 0; i--){
        if (buf.st_mode & 1 << i){
           // sprintf(fullfilename, "%s%c", fullfilename, str[i % 3]);
			sprintf(temp,"%c",str[i % 3]);
			strcat(*fullfilename, temp);
    	}
        else{
           // sprintf(fullfilename, "%s%c", fullfilename, str[3]);
          	sprintf(temp,"%c",str[3]);
			strcat(*fullfilename, temp);
        }
    }


    //打印链接数
   // long link = buf.st_nlink;
   sprintf(temp," %lu ", buf.st_nlink);
   strcat(*fullfilename, temp);

    //打印用户名和组名
    //char pwName[20];
    //char grName[20];
    //strcpy(pwName, getpwuid(buf.st_uid)->pw_name);
    //strcpy(grName, getgrgid(buf.st_gid)->gr_name);

	sprintf(temp, "%s ", getpwuid(buf.st_uid)->pw_name);
	strcat(*fullfilename, temp);
	sprintf(temp, "%s ", getgrgid(buf.st_gid)->gr_name);
	strcat(*fullfilename, temp);

	
	//打印文件大小
    //long int size = buf.st_size;
	sprintf(temp, "%8ldKB ", buf.st_size);
	strcat(*fullfilename, temp);
	
    //打印日期时间
    pt = localtime(&buf.st_mtime);
    //char ptMDTIME[100];
    //snprintf(ptMDTIME, 100, "%d月 %d %02d:%02d ", pt->tm_mon + 1, pt->tm_mday, pt->tm_hour, pt->tm_min);
	sprintf(temp, "%3d月%02d日 %02d:%02d   ",pt->tm_mon+1, pt->tm_mday, pt->tm_hour, pt->tm_min );
	strcat(*fullfilename, temp);

    //打印文件名
//    printf("%s\n", filename);
	strcat(*fullfilename, filename);

  //  snprintf(fullfilename, 1024, "%s%c%lu%s%s%ld%s%s", fullfilename, type, link, pwName, grName, size, ptMDTIME, filename, '\0');
//	printf("%s\n", *fullfilename);

 //   printf("\n");
	
	
    return;
	
}


void do_cli_get_al(int sockfd)
{
    DIR *dir;
    struct dirent *item;
    char filename[50];
    char file_info[1024]; // 用于保存文件属性信息
	char *fullfilename = (char *)malloc(1024);

    dir = opendir(".");
    if (NULL == dir) {
        perror("opendir");
        exit(1);
    }
    printf(RED "server file list for all information :\n" NONE);

    while ((item = readdir(dir)) != NULL) {
        bzero(filename, 50);
        sprintf(filename, "%s", item->d_name); // 修正 sprintf 的使用方式

        if ((strncmp(filename, ".", 1) != 0) && (strncmp(filename, "..", 1) != 0)) {
			bzero(file_info, 1024);
			//printf("File Point22 = %p\n",file_stat(filename));
		    //fullfilename = file_stat(filename); // 获取文件属性信息
		   	file_stat(filename, &fullfilename);
           
            sprintf(file_info, "%s", fullfilename); // 存储文件属性信息到缓冲区
			
            if (send(sockfd, file_info, strlen(file_info), 0) < 0) { // 发送文件属性信息给服务器
               perror("send ls");
                exit(1);
            }
            printf("%s\n", file_info); // 打印文件属性信息到终端
          
        }
    }
	free(fullfilename);
    closedir(dir);
    close(sockfd);
}


*/



